/*
 * @Descripttion: 
 * @version: 
 * @Author: 周志伟
 * @Date: 2021-07-05 09:56:38
 * @LastEditors: 周志伟
 * @LastEditTime: 2021-07-05 11:15:05
 */

'use strict';
const Fs = require('fs');
const Path = require('path');
const whiteList = [
  "componentLib",
  "effect",
  "framework",
  "resources",
  "scenes",
  "shark",
  "sharkSelectQuestion",
  "template",
]

module.exports = {
  load() {
    // execute when package loaded
  },

  unload() {
    // execute when package unloaded
  },

  // register your ipc messages here
  messages: {
    'open'() {
      // open entry panel registered in package.json
      Editor.Panel.open('zzw_change_name');
    },
    'say-hello'() {
      Editor.log('Hello World!');
      // send ipc message to panel
      Editor.Ipc.sendToPanel('zzw_change_name', 'zzw_change_name:hello');
    },
    'clicked'() {
      Editor.log('Button clicked!');
      // 遍历资源目录下的文件
      const assetsPath = Editor.url('db://assets/');
      const documentArr = this.map(assetsPath);

      for (let i = 0; i < documentArr.length; i++) {
        if (whiteList.indexOf(documentArr[i].name) < 0) {
          const str1 = documentArr[i].path;
          const name1 = "A_";
          const name2 = ""
          let str = documentArr[i].path.replace(name1, name2);
          Editor.log("zzzzz", str1, str);

          Fs.rename(str1, str, (err) => {
            Editor.log("zzzzz", err);
          });

        }
      }
    }
  },
  /**
    * 遍历文件/文件夹并执行函数
    * @param {Fs.PathLike} path 路径
    * @param {(filePath: Fs.PathLike, stat: Fs.Stats) => void} handler 处理函数
    */
  map(path) {
    if (!Fs.existsSync(path)) return
    const names = Fs.readdirSync(path);
    const documentArr = [];
    for (const name of names) {
      let _path = Path.join(path, name);
      if (Fs.existsSync(_path)) {
        const stats1 = Fs.lstatSync(_path);
        if (stats1.isDirectory()) {
          const name1 = name.replace(/\s+/g, "");
          let obj = {
            name: name1,
            path: _path
          }
          documentArr.push(obj);
        }
      }
    }
    return documentArr;
  }
};